/*
* OnceFanDesService.java
* Created on  202015/7/22 14:20
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/7/22       panzhuowen    初始版本
*
*/

package com.infitecs.eyas.oncefan.service;

import com.infitecs.eyas.boiler.dao.AirGasParameterDao;
import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.nature.dao.NatureDao;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.oncefan.dao.OnceFanDesDao;
import com.infitecs.eyas.oncefan.model.OnceFanDes;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
@Service
public class OnceFanDesService {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private OnceFanDesDao onceFanDesDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private AirGasParameterDao airGasParameterDao;
    @Autowired
    private NatureDao natureDao;
    @Autowired
    private ElementDao elementDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private OperateFieldDao operateFieldDao;

    public void saveValue(OnceFanDes onceFanDes) {
        onceFanDes.setCaseId(sessionService.getCaseId());
        onceFanDesDao.saveValue(onceFanDes);
        onceFanDes.setParamName(sessionService.getE() + onceFanDes.getParamName());
        sessionService.putParamMap(onceFanDes);
    }

    public OnceFanDes getOnceFanDes() {
        int caseId = sessionService.getCaseId();
        return onceFanDesDao.getOnceFanDes(caseId);
    }

    public void addOnceFan() {
        onceFanDesDao.addOnceFan(sessionService.getCaseId());
    }

    public Double getSelected(int caseId) {
        return onceFanDesDao.getSelected(caseId);
    }

    public void onceFanDesSave(OnceFanDes onceFanDes) {
        if (onceFanDes.getYcfjglState().equals("0")) {
            Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(onceFanDes.getCaseId());
            if (oldEfficiency == null) {
                throw new ValidateErrorException("锅炉数据未填写或未全部填写，不能引用锅炉数据！");
            }
        }
        onceFanDesDataSave(onceFanDes);
    }

    private void onceFanDesDataSave(OnceFanDes onceFanDes) {
        if (onceFanDes.getYcfjglState().equals("0")) {
            AirGasParameter airGasParameter = airGasParameterDao.getAirGasParameter(onceFanDes.getCaseId());
            Efficiency efficiency = efficiencyDao.getEfficiencyByPowerCaseId(onceFanDes.getCaseId());
            //获取锅炉参数
            //E3
            onceFanDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
            //E4
            onceFanDes.setE1(efficiency.getE93());
            //E6
            onceFanDes.setE5(efficiency.getE52());
            //E8
            onceFanDes.setE2(airGasParameter.getE43());
            onceFanDes.setE6(airGasParameter.getWarehouse() + 0.0);
            //20
            onceFanDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
            onceFanDes.setE7(airGasParameter.getOnceWindRate());
            onceFanDes.setE9(airGasParameter.getInOnceWindTemperature());
            onceFanDes.setE8(airGasParameter.getCoalInOnceWindTemperature());
            onceFanDes.setE10(airGasParameter.getOutOnceWindTemperature());
            onceFanDes.setE11(airGasParameter.getE44());
        }
        Nature nature = natureDao.getNatureByPowerCaseId(onceFanDes.getCaseId());
        onceFanDes.setE20(nature.getAnnualAverageTemperature());
        onceFanDes.setE21(nature.getSummerOutdoorCalculateTemperature());
        if (onceFanDes.getE15State().equals("0")) {
            Map<String, Double> paramMap = getOnceFanDesData(onceFanDes);
            onceFanDes.setE15(paramMap.get("E15"));
        }
        OnceFanDes oldData = onceFanDesDao.getOnceFanDes(onceFanDes.getCaseId());
        if (oldData == null) {
            onceFanDesDao.addOnceFanData(onceFanDes);
        } else {
            onceFanDesDao.updateOnceFanData(onceFanDes);
        }
        OperateField operateField = new OperateField();
        operateField.setE15State(onceFanDes.getE15State());
        operateField.setYcfjglState(onceFanDes.getYcfjglState());
        operateField.setCaseId(onceFanDes.getCaseId());
        operateFieldDao.updateOperateFieldOnceFanDes(operateField);

    }

    private Map<String, Double> getOnceFanDesData(OnceFanDes onceFanDes) {
        Map<String, Formula> formulaMap = expressionService.getFormulaMapByType(10);
        Nature nature = natureDao.getNatureByPowerCaseId(onceFanDes.getCaseId());
        Element element = elementDao.getElementByPowerCaseId(onceFanDes.getCaseId());
        Industry industry = industryDao.getIndustryByPowerCaseId(onceFanDes.getCaseId());

        Map<String, Double> paramMap = new HashMap<>();
        paramMap.put("ZE10", nature.getAnnualAverageTemperature());
        paramMap.put("ZE14", nature.getAnnualAverageAirMoisture());
        paramMap.put("ZE15", nature.getLocalDensityMoistAir());
        paramMap.put("ZE16", nature.getSummerOutdoorCalculateTemperature());

        paramMap.put("ME3", industry.getMar());
        paramMap.put("ME26", element.getCar());
        paramMap.put("ME30", element.getSar());
        paramMap.put("ME27", element.getHar());
        paramMap.put("ME28", element.getOar());
        paramMap.put("ME29", element.getNar());
        //E7
        paramMap.put("E1", onceFanDes.getE1());
        paramMap.put("E2", onceFanDes.getE2());
        paramMap.put("E3", onceFanDes.getE3());
        paramMap.put("E4", onceFanDes.getE4());
        paramMap.put("E5", onceFanDes.getE5());
        paramMap.put("E6", onceFanDes.getE6());
        paramMap.put("E7", onceFanDes.getE7());
        paramMap.put("E8", onceFanDes.getE8());
        paramMap.put("E9", onceFanDes.getE9());
        paramMap.put("E10", onceFanDes.getE10());
        paramMap.put("E11", onceFanDes.getE11());
        paramMap.put("E18", onceFanDes.getE18());
        paramMap.put("E19", onceFanDes.getE19());
        paramMap.put("E20", onceFanDes.getE20());
        paramMap.put("E21", onceFanDes.getE21());
        paramMap.put("E42", onceFanDes.getE42());
        paramMap.put("E52", onceFanDes.getE52());
        paramMap.put("E63", onceFanDes.getE63());
        //E12
        double E12 = expressionService.getCalculateData(formulaMap.get("CE12"), paramMap);
        paramMap.put("E12", E12);
        //E13
        double E13 = expressionService.getCalculateData(formulaMap.get("CE13"), paramMap);
        paramMap.put("E13", E13);
        //E14
        double E14 = expressionService.getCalculateData(formulaMap.get("CE14"), paramMap);
        paramMap.put("E14", E14);
        //E15
        double E15;
        Double E6 = paramMap.get("E6");
        if (E6 == 1) {
            E15 = expressionService.getCalculateData(formulaMap.get("CE15_1"), paramMap);
        } else {
            E15 = expressionService.getCalculateData(formulaMap.get("CE15_2"), paramMap);
        }
        paramMap.put("E15", E15);
        return paramMap;
    }
}
